﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlyCapture2Managed;

namespace DualChannel_v2._0
{
    public partial class CameraPropertyControl : UserControl
    {
        SavebleCameraProperty cp;
        CameraPropertyInfo cpi;
        PropertyType pt;
        public delegate void PropChangeEvent(CameraPropertyControl sender, CameraProperty cp);
        public event PropChangeEvent PropChanged; 

        bool InternalUpdate;

        public CameraPropertyControl()
        {
            InitializeComponent();


        }



        public CameraPropertyControl(SavebleCameraProperty cp, CameraPropertyInfo cpi)
        {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            //SetStyle(ControlStyles.
            this.cp = cp;
            this.cpi = cpi;
            BackColor = Color.Transparent;
            pt = cp.type;

            //PropVal.BackColor = Color.Transparent;
            //PropTrackVal.BackColor = Color.Transparent;

            PropVal.BorderStyle = System.Windows.Forms.BorderStyle.None;

            RefreshData(cp, cpi);
        }




        public void RefreshData(SavebleCameraProperty cp, CameraPropertyInfo cpi)
        {
            InternalUpdate = true;
            this.cp = cp;
            this.cpi = cpi;
            pt = cp.type;

            PropName.Text = cp.type.ToString();
            PropUnits.Text = cpi.unitAbbr;
            if (cpi.absValSupported)
            {
                try
                {
                    if (double.IsInfinity(cpi.absMax))
                        PropVal.Maximum = 1000;
                    else
                    PropVal.Maximum = (decimal)cpi.absMax;
                    PropVal.Minimum = (decimal)cpi.absMin;

                    PropVal.Value = (decimal)cp.absValue;
                }
                catch { PropVal.Value = PropVal.Minimum; }



            }
            else
            //PropVal.Value = (decimal)cp.valueA;
            {
                PropVal.Maximum = (decimal)4095;
                PropVal.Minimum = (decimal)0;

                PropVal.Value = (decimal)cp.valueA;
            
            }                
            PropMax.Text = PropVal.Maximum.ToString();
                PropMin.Text = PropVal.Minimum.ToString();

                PropAuto.Enabled = cpi.autoSupported;
                PropAuto.Checked = cp.autoManualMode;

                PropOnePush.Enabled = cpi.onePushSupported;
                PropOnePush.Checked = cp.onePush;

                OnOff.Enabled = cpi.onOffSupported;
                OnOff.Checked = cp.onOff;
                try
                {
                    PropTrackVal.Value = (int)((PropVal.Value - PropVal.Minimum) / (PropVal.Maximum - PropVal.Minimum) * 99);
                }
                catch
                {
                    PropTrackVal.Value = 0;
                }

            InternalUpdate = false;
            
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            if (!InternalUpdate)
                PropVal.Value = PropVal.Minimum + (PropVal.Maximum - PropVal.Minimum) / (decimal)100 * (decimal)PropTrackVal.Value;
        }


        private void OnOff_CheckedChanged(object sender, EventArgs e)
        {
                    SubmitChanges();
        }

        public void SubmitChanges()
        {
            CameraProperty cp1 = new CameraProperty(cp.type);

            if(cpi.absValSupported)
            cp1.absValue = (float)PropVal.Value;
            else
                cp1.valueA = (uint)Math.Abs(PropVal.Value);
            cp1.absControl = cpi.absValSupported;
            cp1.onePush = PropOnePush.Checked;
            cp1.onOff = OnOff.Checked;
            cp1.autoManualMode = PropAuto.Checked;

            if (PropChanged != null)
                if (PropChanged.Method != null)
                    PropChanged(this, cp1);


        }

        private void PropVal_ValueChanged(object sender, EventArgs e)
        {
            SubmitChanges();
        }


    }
}
